package com.rz.paas.test.SettingDetail

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.orhanobut.hawk.Hawk
import com.rz.paas.test.R
import com.rz.paas.test.base.MiddleActivity
import com.rz.paas.test.constant.AppConstant
import kotlinx.android.synthetic.main.activity_custom_push.*
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.custom_action_bar.*

class CustomPushActivity : MiddleActivity() {
    companion object {
        const val CUSTOM_PUSH = "OPTION_ID"
        fun open(context: Activity, customPush: Boolean) {
            val intent = Intent(context, CustomPushActivity::class.java)
            intent.putExtra(CUSTOM_PUSH, customPush)
            context.startActivityForResult(intent,2001)
        }
    }

    private val TAG = "CustomPushActivity"
    val dataMap = AppConstant.dataMap
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_custom_push)

        val customPush = intent.getBooleanExtra(CUSTOM_PUSH, false)

        initText(customPush)

        push_mode_select.setOnClickListener {
            val optionIndex = AppConstant.PUSH_SCREEN_MODE_DATA_INDEX
            SettingDetailActivity.open(
                this, optionIndex, Hawk.get(dataMap[optionIndex].name) ?: 0, getSubTitle(customPush)
            )
        }

        encode_resolution_select.setOnClickListener {
            val optionIndex = AppConstant.ENCODE_RESOLUTION_DATA_INDEX
            SettingDetailActivity.open(
                this, optionIndex, Hawk.get(dataMap[optionIndex].name) ?: 0, getSubTitle(customPush)
            )
        }

        encode_fps_select.setOnClickListener {
            val optionIndex = AppConstant.ENCODE_FPS_DATA_INDEX
            SettingDetailActivity.open(
                this, optionIndex, Hawk.get(dataMap[optionIndex].name) ?: 0, getSubTitle(customPush)
            )
        }

        encode_direction_select.setOnClickListener {
            val optionIndex = AppConstant.ENCODE_DIRECTION_DATA_INDEX
            SettingDetailActivity.open(
                this, optionIndex, Hawk.get(dataMap[optionIndex].name) ?: 0, getSubTitle(customPush)
            )
        }


    }

    private fun getSubTitle(customPush: Boolean): String {
        return if (customPush) {
            "推流二"
        } else {
            "推流二"
        }
    }

    private fun initText(customPush: Boolean) {
        sub_title.text = "设置"

        if (customPush) {
            main_title.text = "推流二"

        } else {
            main_title.text = "推流二"
            push_type_setting.text = "默认推流"
            video_type.text = "视频1"
            custom_push_switch.visibility = View.GONE
        }
        initText()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            var optionId = data!!.getIntExtra(SettingDetailActivity.OPTION_ID, 0)
            var stringArray = resources.getStringArray(dataMap[optionId].resId)
            when (optionId) {
                AppConstant.PUSH_SCREEN_MODE_DATA_INDEX ->
                    current_push_mode.text = stringArray[Hawk.get(dataMap[optionId].name)]
                AppConstant.ENCODE_RESOLUTION_DATA_INDEX ->
                    current_encode_resolution.text = stringArray[Hawk.get(dataMap[optionId].name)]
                AppConstant.ENCODE_FPS_DATA_INDEX ->
                    current_encode_fps.text = stringArray[Hawk.get(dataMap[optionId].name)]
                AppConstant.ENCODE_DIRECTION_DATA_INDEX ->
                    current_encode_direction.text = stringArray[Hawk.get(dataMap[optionId].name)]

            }
        }
    }

    private fun initText() {
        current_push_mode.text = getResString(AppConstant.PUSH_SCREEN_MODE_DATA_INDEX)
        current_encode_resolution.text = getResString(AppConstant.ENCODE_RESOLUTION_DATA_INDEX)
        current_encode_fps.text = getResString(AppConstant.ENCODE_FPS_DATA_INDEX)
        current_encode_direction.text = getResString(AppConstant.ENCODE_DIRECTION_DATA_INDEX)
    }

    private fun getResString(dataIndex: Int): String {
        var stringArray = resources.getStringArray(dataMap[dataIndex].resId)
        return stringArray[Hawk.get(dataMap[dataIndex].name,0)]
    }
}